package com.hdoit.ares.web.insu.wechat;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.hdoit.ares.logical.acc.model.WithdrawParam;
import com.hdoit.ares.logical.acc.service.IWithdrawParamService;
import com.hdoit.ares.logical.cust.model.Customer;
import com.hdoit.ares.logical.cust.service.ICustomerService;
import com.hdoit.ares.logical.insu.model.Insurance;
import com.hdoit.ares.logical.insu.model.InsuranceCompany;
import com.hdoit.ares.logical.insu.model.InsurancePeople;
import com.hdoit.ares.logical.insu.service.IInsuranceCompanyService;
import com.hdoit.ares.logical.insu.service.IInsurancePeopleService;
import com.hdoit.ares.logical.insu.service.IInsuranceService;
import com.hdoit.ares.logical.viewmodel.insu.FindInsu;
import com.hdoit.ares.selfcommon.enums.AresManageContainer;
import com.hdoit.ares.selfcommon.thirdparty.zzb.utils.RC4Util;
import com.hdoit.ares.selfcommon.thirdparty.zzb.utils.ZzbConfiguration;
import com.hdoit.ares.selfcommon.thirdparty.zzb.utils.ZzbManageContainer;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.logical.setting.model.ParamOption;
import com.tennetcn.common.message.DaoBaseRuntimeException;
import com.tennetcn.common.message.WechatLoginModel;
import com.tennetcn.component.wechat.BaseWechat;
import com.tennetcn.web.cbase.AuthPassport;

@Controller
@RequestMapping(value = "/wechat/insu/insuranceClassify/")
@AuthPassport
public class InsuranceClassifyWechat extends BaseWechat{
	
	@Override
	public String getRoutePath() {
		return "areas/wechat/insu/insuranceClassify/";
	}
	
	private IInsuranceService insuranceService;
	private IInsuranceCompanyService companyService;
	private IInsurancePeopleService peopleService;
	private ICustomerService customerService;
	private IWithdrawParamService withdrawParamService;

	public ICustomerService getCustomerService() {
		return customerService;
	}
	
	@Resource
	public void setCustomerService(ICustomerService customerService) {
		this.customerService = customerService;
	}
	public IInsuranceService getInsuranceService() {
		return insuranceService;
	}
	@Resource
	public void setInsuranceService(IInsuranceService insuranceService) {
		this.insuranceService = insuranceService;
	}

	public IInsuranceCompanyService getCompanyService() {
		return companyService;
	}	
	@Resource
	public void setCompanyService(IInsuranceCompanyService companyService) {
		this.companyService = companyService;
	}
	
	public IInsurancePeopleService getPeopleService() {
		return peopleService;
	}
	@Resource
	public void setPeopleService(IInsurancePeopleService peopleService) {
		this.peopleService = peopleService;
	}
	
	public IWithdrawParamService getWithdrawParamService() {
		return withdrawParamService;
	}
	@Resource
	public void setWithdrawParamService(IWithdrawParamService withdrawParamService) {
		this.withdrawParamService = withdrawParamService;
	}
	
	private final String showInsuViewName = "showInsu";
	private final String insulistViewName = "insulist";
	private final String insuranceWechatDetailViewName = "insuranceWechatDetail";
	private final String insuranceClassifyViewName="insuranceClassify";
	private final String productDetailViewName = "productDetail";

	
	@RequestMapping(value = insuranceWechatDetailViewName, method = { RequestMethod.GET,RequestMethod.POST })
	public String iframeDetail(Model model,String insuranceId,String userId){
		WechatLoginModel loginModel = wechatSession.getCurrentLogin();
		Customer customer =null;
		Insurance insurance=null;
		
		//众安产品 链接
		String key = ZzbConfiguration.getInstance().getValue(ZzbManageContainer.zhongan_openKey);
		String data = "{\"userID\":\"" + loginModel.getLoginId() + "_"+ insuranceId + "\"}";
		String sign = RC4Util.encryRC4String(data, key);
		model.addAttribute("bizContent", sign);
		
		if(loginModel!=null){
			customer = (Customer)loginModel.get(AresManageContainer.customer_account_info);
			/*如果保险id不为空的话，说明是从我方产品列表进来的*/
			if(!StringUtils.isEmpty(insuranceId)){
				insurance = insuranceService.queryModel(insuranceId);
				userId = customer.getId()+"_"+insurance.getId();
				model.addAttribute("userId", userId);
				model.addAttribute("insurance", insurance);
				return view(insuranceWechatDetailViewName);
			}
			/*如果userId不为空，说明是点击分享的链接进来的*/
			if(!StringUtils.isEmpty(userId)){
				System.out.println("fanhuachuanguolaide===userId:============================================================================================"+userId);
				if(userId.indexOf("_")<0){
					throw new DaoBaseRuntimeException("userId is incompatible with the regulation");
				}
				System.err.println(userId);
				String[] userIdentityAndProdId = userId.split("_");
				String userIdentity = userIdentityAndProdId[0];
				String prodId = userIdentityAndProdId[1];
				
				if(!StringUtils.isEmpty(userIdentity)){
					/*说明这个人没有上级，把分享人存到他的购买推荐人当中*/
					if(customer.getRefereeIdentity()<=0){
						Customer refreeCustomer = customerService.queryModelByUserIdentity(Integer.parseInt(userIdentity));
						customer.setBuyRefreeIdentity(refreeCustomer.getUserIdentity());
						customer.setModelStatus(ModelStatus.update);
						customerService.applyChange(customer);
					}
				}
				if(!StringUtils.isEmpty(prodId)){
					insurance = insuranceService.queryModel(prodId);
					userId = customer.getUserIdentity()+"_"+insurance.getId();
					model.addAttribute("userId", userId);
					model.addAttribute("insurance", insurance);
					return view(insuranceWechatDetailViewName);
				}	
			}
		}
		
			return view(insuranceWechatDetailViewName);
	}

	@RequestMapping(value = showInsuViewName, method = { RequestMethod.GET,RequestMethod.POST })

	public String showInsu(Model model){
		
		/*测试数据                                                                                                              正式数据
		4775b72b-f558-488f-bdf4-4c9fda995a20 健康险 	3533ef78-b68b-42dd-a495-52b814efa33f
		36866826-1553-4eb7-9ab1-bee20b8f4666 意外险           1ff92d07-d3bb-45df-9104-4708917958bb
		6c58055d-0a3a-428d-ae38-9787693c34fc 财产险         72edc2e8-cde4-403d-bee7-14353d844394
		*/
		FindInsu findInsu = new FindInsu();

		//查意外险的二级菜单
		List<ParamOption> healthOptions = insuranceService.queryList("3533ef78-b68b-42dd-a495-52b814efa33f");
		List<ParamOption> moneyOptions = insuranceService.queryList("72edc2e8-cde4-403d-bee7-14353d844394");
		List<ParamOption> accOptions = insuranceService.queryList("1ff92d07-d3bb-45df-9104-4708917958bb");
		
		WechatLoginModel loginModel=wechatSession.getCurrentLogin();
		Customer customer=(Customer)loginModel.get(AresManageContainer.customer_account_info);
				
		List<InsuranceCompany> companys = companyService.queryList();
		List<InsurancePeople> peoples = peopleService.queryList();
		
		WithdrawParam pageSize = withdrawParamService.queryModelByHelpCode("pageSize");	
		List<Insurance> insurances = new ArrayList<Insurance>();
		findInsu.setInsuOne("");
		insurances = insuranceService.queryListByFindInsu(findInsu);
		int allPage =(int)Math.ceil((double)insurances.size()/(double)pageSize.getParam());
		
		findInsu.setInsuOne("意外险");
		insurances = insuranceService.queryListByFindInsu(findInsu);
		int accPage =(int)Math.ceil((double)insurances.size()/(double)pageSize.getParam());
		
		findInsu.setInsuOne("健康险");
		insurances = insuranceService.queryListByFindInsu(findInsu);
		int healthPage =(int)Math.ceil((double)insurances.size()/(double)pageSize.getParam());
		
		findInsu.setInsuOne("财产险");
		insurances = insuranceService.queryListByFindInsu(findInsu);
		int MoneyPage =(int)Math.ceil((double)insurances.size()/(double)pageSize.getParam());

		model.addAttribute("healthOptions", healthOptions);	
		model.addAttribute("moneyOptions", moneyOptions);	
		model.addAttribute("accOptions", accOptions);	
		model.addAttribute("customer", customer);	
		model.addAttribute("companys",companys);
		model.addAttribute("peoples",peoples);
		model.addAttribute("allPage", allPage);
		model.addAttribute("accPage", accPage);
		model.addAttribute("healthPage", healthPage);
		model.addAttribute("MoneyPage", MoneyPage);

		return view(showInsuViewName);
	}

	@RequestMapping(value = insulistViewName, method = { RequestMethod.GET,RequestMethod.POST })
	public String insulist(Model model,FindInsu findInsu){
		WechatLoginModel loginModel=wechatSession.getCurrentLogin();
		Customer customer=(Customer)loginModel.get(AresManageContainer.customer_account_info);
		List<Insurance> insurances = insuranceService.queryListByFindInsu(findInsu);
		
		model.addAttribute("insurances", insurances);
		model.addAttribute("customer",customer);

		return view(insulistViewName);
	}

	@RequestMapping(value = insuranceClassifyViewName, method = { RequestMethod.GET,RequestMethod.POST })
	public String insuranceClassify(Model model){
		return view(insuranceClassifyViewName);
	}
	
	@RequestMapping(value = productDetailViewName, method = { RequestMethod.GET,RequestMethod.POST })
	public String productDetail(Model model){
		return view(productDetailViewName);
	}
	
}
